class ContentsController < ApplicationController
  
  def write_now
    @page = Page.find(params[:id])
    render :update do |page|
      page << "goToMenu('write_content_menu');"
      page.replace_html "widgets", :partial => "write_now"
#      page.insert_html :bottom, "page_actions", :partial => "write_now"
    end
  end
  
  def show_library
    @page = Page.find(params[:id])
    @contents = Content.find :all
    render :update do |page|
      page << "goToMenu('write_content_menu');"
      page.replace_html "widgets", :partial => "show_library"
    end
  end
  
  def live_search (search_text = params[:searchtext])
    @phrase = search_text
    @contents = Content.find(:all, :conditions => "( contents.body LIKE '%#{@phrase}%' )")
    s = render_to_string( :partial => "contents_list" )
    render :update do |page|
      page.alert(s)#:todo:debug:
      page["select_content"].replace_html :partial => "contents_list"
      page["search_comment"].replace_html( @phrase==""?"":"#{@contents.length.to_s} result#{'s' unless @contents.length==1} for <code>#{@phrase}</code>" )
      page["content_actions"].hide
    end
  end
  
  def content_preview
    unless params[:select_content]==""
      @content = Content.find params[:select_content]
      render :partial=>"content_preview"
    else
      render :text => %{<i style="color:lightgrey">no content selected for the preview</i>}
    end
  end
  
  def delete_content
    @content = Content.find params[:content][:id]
    @page_elements = @content.page_elements.find_all "page_id = #{params[:id]}"
    elements_to_remove = @page_elements.collect {|page_element|
      if page_element.page_id.to_s == params[:id]
        'element_'<< page_element.id.to_s
      end
      page_element.destroy
    }.compact
    
    render :update do |page|
      if elements_to_remove
        elements_to_remove.each{ |e| page["element_"+e.id.to_s].remove } 
      end
      page<< remote_function( 
              :url => { :action => :live_search }, 
              :with => "('searchtext='+$('searchtext').value)" )
    end
    @content.destroy
  end
  
  def create_page_element
    @page = Page.find(params[:id])
    @page_element = PageElement.create(:area_name=>"main", :page_id=>params[:id])
    if params[:content][:id]
      @page_element.content = Content.find(params[:content][:id])
    else
      @page_element.content = Content.create(params[:content])
    end
    if @page_element.save
      element_html = %(<div class='compages_element' id='element_#{ @page_element.id }'>)
      element_html << @page_element.content.body_to_html
      element_html << %(</div>)
      render :update do |page|
      #qui problemi con IE :todo:
        page.replace_html "widgets", ""
        page.insert_html :top, "area_main", element_html
        area = "main"
        page.sortable( "area_"<< area, 
                          :url=>{ :controller=>"contents", :action=>"update_area_elements", :id=>("area_"+area) },
                          :dropOnEmpty=>true, 
                          :tag=>:div, 
                          :only=>"compages_element",
                          :constraint=>false, 
                          :hoverclass=>"\"dragging\"",
                          :containment=>@page.area_names.collect{|name| "area_"<< name})
        page["element_#{ @page_element.id }"].visual_effect :highlight
        page<< "goToMenu('add_content_menu');"
      end
    else
      @page_element.destroy
      render :text=>params.inspect
    end
  end
  
  def update_area_elements
    position = 0
    area_name = params[:id]
    for page_element_id in params[area_name]
      page_element = PageElement.find page_element_id
      page_element.update_attribute(:position, position)
      page_element.update_attribute(:area_name, area_name["area_".length..-1])
      position += 1
    end
    render :nothing=>true
  end
  
  def remove_page_element( element_id = params[:selected_element]["element_".length..-1] )
    PageElement.find( element_id ).destroy
    render :update do |page|
      page.remove "element_"+element_id
      page['remove_element'].hide
    end
  end
  def edit_page_element( element_id = params[:selected_element]["element_".length..-1] )
    @page_element = PageElement.find( element_id )
    @content = @page_element.content
    render :update do |page|
      page['compages_bar_wrapper'].visual_effect :appear, :duration=>0.2
      page['restore_bar'].hide
      page['hide_bar'].visual_effect :pulsate, :duration=>2
      page << "goToMenu('edit_content_menu');"
      page.replace_html "widgets", :partial => "edit_content"
    end
  end
  
  def update_page_elements
    @content = Content.find( params[:content][:id] )
    @content.update_attributes params[:content]
    @page_elements = @content.page_elements.find_all "page_id = #{params[:id]}"

    
    render :update do |page|
      @page_elements.each{ |e|
        page['element_'<< e.id.to_s].replace_html @content.body_to_html
      } if @page_elements
      page['compages_bar_wrapper'].visual_effect :fade, :duration=>0.2
      page['restore_bar'].show
      page << "goToMenu('main_menu');"
      page.replace_html "widgets", :text => ""
    end
  end
end
